package com.jhf.skypac_pda.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.adapter.interfaces.BaseAdapter;
import com.jhf.skypac_pda.model.ExceptionListModel;

import java.util.List;

public class ExceptionListAdapter extends BaseAdapter<ExceptionListAdapter.ExceptionListItemHolder> {
    private List<ExceptionListModel> mList;
    private Context context;
    private String orderType = "";


    public ExceptionListAdapter(Context mContext, @NonNull List<ExceptionListModel> mList, String orderType) {
        this.mList = mList;
        this.context = mContext;
        this.orderType = orderType;
    }

    @Override
    public ExceptionListAdapter.ExceptionListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.exception_list_item, parent, false);
        ExceptionListAdapter.ExceptionListItemHolder holder = new ExceptionListAdapter.ExceptionListItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ExceptionListAdapter.ExceptionListItemHolder holder, final int position) {
        holder.ll_order_detail.setVisibility(View.GONE);
        holder.tv_type_exception.setText(mList.get(position).getExceptionType());
        holder.tv_warehouse_position.setText(mList.get(position).getWarehousePosition());
        holder.num_pallet.setText(mList.get(position).getPalletNum());


        holder.tv_exception_time.setText(mList.get(position).getExceptionTime());
        holder.tv_excuse_exception.setText(mList.get(position).getExceptionExcuse());

        holder.tv_status.setText(mList.get(position).getStatus());
        //Log.i("===========", "onBindViewHolder处理状态是: " + orderType);
        if (orderType.equals("4")) {
            holder.ll_order_detail_right.setVisibility(View.GONE);
            holder.right_menu_1.setVisibility(View.GONE);
            holder.right_menu_2.setVisibility(View.GONE);
        } else {
            holder.ll_order_detail_right.setVisibility(View.VISIBLE);
            holder.right_menu_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mViewClickListener != null) {
                        mViewClickListener.post_re_importInventory(position);
                        holder.easySwipeMenuLayout.resetStatus();
                    }
                }
            });
            holder.right_menu_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mViewClickListener != null) {
                        mViewClickListener.post_manualHandling(position);
                        holder.easySwipeMenuLayout.resetStatus();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ExceptionListItemHolder extends RecyclerView.ViewHolder {
        TextView tv_type_exception;
        TextView tv_warehouse_position;
        TextView num_pallet;
        TextView tv_exception_time;
        TextView tv_excuse_exception;
        TextView tv_status;
        TextView right_menu_1;
        TextView right_menu_2;
        LinearLayout ll_order_detail;
        LinearLayout ll_order_detail_right;
        EasySwipeMenuLayout easySwipeMenuLayout;

        public ExceptionListItemHolder(View itemView) {
            super(itemView);
            tv_type_exception=itemView.findViewById(R.id.tv_type_exception);
            tv_warehouse_position=itemView.findViewById(R.id.tv_warehouse_position);
            num_pallet=itemView.findViewById(R.id.num_pallet);
            tv_exception_time=itemView.findViewById(R.id.tv_exception_time);
            tv_excuse_exception=itemView.findViewById(R.id.tv_excuse_exception);
            tv_status=itemView.findViewById(R.id.tv_status);
            ll_order_detail=itemView.findViewById(R.id.left);
            ll_order_detail_right=itemView.findViewById(R.id.right);
            right_menu_1=itemView.findViewById(R.id.right_menu_1);
            right_menu_2=itemView.findViewById(R.id.right_menu_2);
            easySwipeMenuLayout=itemView.findViewById(R.id.es);
        }
    }

    public interface ViewClickListener{
        void post_re_importInventory(int pos);
        void post_manualHandling(int pos);
    }

    public void setViewClickListener(ExceptionListAdapter.ViewClickListener mViewClickListener){
        this.mViewClickListener=mViewClickListener;

    }


    private ExceptionListAdapter.ViewClickListener mViewClickListener;

}
